import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import login from '@/views/login/login.vue'
import main from '@/views/main.vue'
import localCache from '@/utils/cache'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: login
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '@/views/login/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import( '@/views/main/main.vue'),
    // children:[] ->根据usermeuns决定 
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import( '@/views/not-found/not-found.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})


//
router.beforeEach((to)=>{
  // 判断要来的路径不是登陆  （如果要跳的不是登录页 要判断有没有token 如果有就跳 没有返回登录页）
  if(to.path !== '/login'){
      const token =localCache.getCache('token')
      if(!token){
        return '/login'
      }
  }
})

export default router
